/*
 * @Copyright: Copyright (C) AMCAX, All rights reserved.
 * @Author: 殷建峰
 * @Date: 2024-03-28 09:33:17
 * @LastEditTime: 2025-03-04 15:22:44
 * @LastEditors: 殷建峰
 * @Description:
 */
// import { Modal } from 'antd';
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from '@mui/material';
import { useImperativeHandle, useState } from 'react';
import { userEvents } from '@app-base/module';
import { CommandEdit } from '../components';
import { AI_EVENTS } from '../events';

const CommandEditDialog = (props: any) => {
  const [isVisible, setVisible] = useState(false);
  const [content, setContent] = useState('');

  useImperativeHandle(props.onRef, () => ({
    show,
    close,
  }));

  const show = () => {
    setVisible(true);
  };
  const close = () => {
    setVisible(false);
  };

  const handleOk = () => {
    const item = props.item;
    item.content = content;
    userEvents.emit(AI_EVENTS.AI_COMMAND_UPDATE, item);
    close();
  };

  const handleCancel = () => {
    close();
  };

  const handleContentChange = (content) => {
    setContent(content);
  };

  return (
    <>
      <Dialog
        open={isVisible}
        onClose={handleCancel}
        maxWidth='md'
        fullWidth
      >
        <DialogTitle>编辑指令</DialogTitle>
        <DialogContent
          sx={{
            overflow: 'hidden',
          }}
        >
          <CommandEdit
            item={props.item}
            onChange={handleContentChange}
          />
        </DialogContent>
        <DialogActions>
          <Button onClick={handleCancel}>取消</Button>
          <Button onClick={handleOk}>确定</Button>
        </DialogActions>
      </Dialog>
    </>
  );
};

export default CommandEditDialog;
